package cn.stylefeng.guns.modular.tool.controller;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.tool.entity.ToolBasicStopFiring;
import cn.stylefeng.guns.modular.tool.model.params.ToolBasicStopFiringParam;
import cn.stylefeng.guns.modular.tool.service.ToolBasicStopFiringService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;


/**
 * 停发地区设置控制器
 */
@Controller
@RequestMapping("/toolBasicStopFiring")
public class ToolBasicStopFiringController extends BaseController {

    private String PREFIX = "/tool/toolBasicStopFiring";

    @Autowired
    private ToolBasicStopFiringService toolBasicStopFiringService;

    @Autowired
    private UserService userService;
    
    /**
     * 跳转到导入页面
     */
    @RequestMapping("toimportExcel")
    public String toimportExcel() {
        return PREFIX + "/import.html";
    }
    
    /**
     * 下载订单模板
     * @throws ServletException , IOException
     */
    @RequestMapping("downloadExcelTemplate")
    public void downloadExcelTemplate(HttpServletRequest request,HttpServletResponse response) throws ServletException ,IOException {
        String path = "assets/excel/toolBasicStopFiring.xlsx";
        try {
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(path);
            OutputStream  outputStream = response.getOutputStream();
            //设置下载的文件类型
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            //设置下载文件名
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(("停发地区模板" + ".xlsx").getBytes(), "iso-8859-1"));
            IOUtils.copy(inputStream, outputStream);
            outputStream.flush();
            inputStream.close();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private Lock lock = new ReentrantLock();
    
    /**
     * 上传excel   
     */
	@RequestMapping(value = "/importExceNewmethodl" , method = {RequestMethod.POST} )
    @ResponseBody
    public ResponseData importExceNewmethodl(@RequestPart("file") MultipartFile file)  {
		ResponseData data = new ResponseData();
		lock.lock();//获取锁
		try {
			data = toolBasicStopFiringService.getExceldata(file,data);//处理导入数据
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			lock.unlock();//释放锁
		}
        return data;
    }

    /**
     * 跳转到主页面
     *
     * @author 
     * @Date 2022-08-05
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/toolBasicStopFiring.html";
    }

    /**
     * 新增页面
     *
     * @author 
     * @Date 2022-08-05
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/toolBasicStopFiring_add.html";
    }

    /**
     * 编辑页面
     *
     * @author 
     * @Date 2022-08-05
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/toolBasicStopFiring_edit.html";
    }

    /**
     * 新增接口
     *
     * @author 
     * @Date 2022-08-05
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ToolBasicStopFiringParam toolBasicStopFiringParam) {
        String address = toolBasicStopFiringParam.getProvince();
        if(ToolUtil.isNotEmpty(toolBasicStopFiringParam.getCity())) {
        	address += toolBasicStopFiringParam.getCity();
        	if(ToolUtil.isNotEmpty(toolBasicStopFiringParam.getAreas())) {
            	address += toolBasicStopFiringParam.getAreas();
            	if(ToolUtil.isNotEmpty(toolBasicStopFiringParam.getStreet())) {
                	address += toolBasicStopFiringParam.getStreet();
            	}
        	}
        }
        toolBasicStopFiringParam.setAddress(address);
    	toolBasicStopFiringParam.setStates("1");
    	toolBasicStopFiringParam.setDeptId(userService.getDeptId());
        this.toolBasicStopFiringService.add(toolBasicStopFiringParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author 
     * @Date 2022-08-05
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ToolBasicStopFiringParam toolBasicStopFiringParam) {
        String address = toolBasicStopFiringParam.getProvince();
        if(ToolUtil.isNotEmpty(toolBasicStopFiringParam.getCity())) {
        	address += toolBasicStopFiringParam.getCity();
        	if(ToolUtil.isNotEmpty(toolBasicStopFiringParam.getAreas())) {
            	address += toolBasicStopFiringParam.getAreas();
            	if(ToolUtil.isNotEmpty(toolBasicStopFiringParam.getStreet())) {
                	address += toolBasicStopFiringParam.getStreet();
            	}
        	}
        }
        toolBasicStopFiringParam.setAddress(address);
        this.toolBasicStopFiringService.update(toolBasicStopFiringParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author 
     * @Date 2022-08-05
     */
    @RequestMapping("/editItem2")
    @ResponseBody
    public ResponseData editItem2(ToolBasicStopFiringParam toolBasicStopFiringParam) {
        this.toolBasicStopFiringService.update(toolBasicStopFiringParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author 
     * @Date 2022-08-05
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ToolBasicStopFiringParam toolBasicStopFiringParam) {
        this.toolBasicStopFiringService.delete(toolBasicStopFiringParam);
        return ResponseData.success();
    }

    /**
     * 批量删除接口
     */
    @RequestMapping("/btnRevoke")
    @ResponseBody
    public ResponseData btnRevoke(Integer[] ids) {
    	for (Integer id : ids) {
    		ToolBasicStopFiringParam toolExpressParam = new ToolBasicStopFiringParam();
    		toolExpressParam.setId(id);
			this.toolBasicStopFiringService.delete(toolExpressParam);
		}
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author 
     * @Date 2022-08-05
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(ToolBasicStopFiringParam toolBasicStopFiringParam) {
        ToolBasicStopFiring detail = this.toolBasicStopFiringService.getById(toolBasicStopFiringParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author 
     * @Date 2022-08-05
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(ToolBasicStopFiringParam toolBasicStopFiringParam,String condition ) {
    	User user = userService.getcurrentUser();
    	toolBasicStopFiringParam.setDeptId(user.getDeptId());
        return this.toolBasicStopFiringService.findPageBySpec(toolBasicStopFiringParam,condition,user.getOperateId());
    }

}


